package com.itheima.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.resource.MessageConstant;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckItem checkItem) {

        try {
            checkItemService.add(checkItem);
            //没有异常返回Result实体类信息
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //出现异常
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }

    /**
     * 条件查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            Map map = checkItemService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }


    }


    /**
     * 删除检查项
     * @param id
     * @return
     */
    @RequestMapping("/deleteCheckItemById.do")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteCheckItemById(@RequestParam("id") Integer id){

        try {
            checkItemService.deleteCheckItemById(id); //逻辑删除成功
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/edit.do")
    public Result editCheckItem(@RequestBody CheckItem checkItem){

        try {
            checkItemService.edit(checkItem); //逻辑删除成功
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }

    }


    /**
     * 查询所有检查项
     * @return
     */
    @RequestMapping("/findAll.do")
    public Result findAll(){
        try {
            List<CheckItem> itemList = checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,itemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
